<template>
  <el-form ref="formRef" :model="form" label-width="120px" :rules="rules">
    <el-form-item label="部门名称" is-required prop="name">
      <el-input v-model="form.name" style="width: 80%" placeholder="请输入部门名称" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width: 80%" placeholder="请输入部门编码" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" placeholder="请选择部门负责人" style="width: 80%">
        <el-option v-for="item in list" :key="item.id" :label="item.username" :value="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" type="textarea" style="width: 80%" placeholder="1-300个字符" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>

</template>

<script>
import { addDepartments, editDepartmentsById, getDepartmentsById, getDialogOptionList } from '@/api/departments'
export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    isList: {
      type: Array,
      required: true
    }
  },
  data() {
    const existCodeValid = (rule, value, callback) => {
      let existCodeList = this.isList.map(item => item.code)
      if (this.isEdit) {
        existCodeList = this.isList.filter(item => item.id !== this.id).map(item => item.code)
      }
      if (existCodeList.includes(value)) {
        callback(new Error(`部门编码：${value}已存在`))
      } else {
        callback()
      }
    }
    const existNameValid = (rule, value, callback) => {
      let existNameList = this.isList.filter(item => item.pid === this.id).map(item => item.name)
      if (this.isEdit) {
        const pid = this.isList.find(item => item.id === this.id).pid
        const brothers = this.isList.map(item => item.pid === pid)
        existNameList = brothers.filter(item => item.id !== this.id).map(item => item.name)
      }
      if (existNameList.includes(value)) {
        callback(new Error(`部门名称：${value}已存在`))
      } else {
        callback()
      }
    }
    return {
      // 自定义校验

      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', triggle: 'blur' },
          { min: 1, max: 20, message: '请输入1-20', triggle: 'blur' },
          { validator: existNameValid, triggle: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', triggle: 'blur' },
          { min: 1, max: 20, message: '请输入1-20', triggle: 'blur' },
          { validator: existCodeValid, triggle: 'blur' }
        ],
        manager: [
          { required: true, message: '请选择负责人', triggle: 'change' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', triggle: 'blur' },
          { min: 1, max: 300, message: '请输入1-300', triggle: 'blur' }
        ]
      },
      list: []
    }
  },
  created() {
    this.loadgetDialogOptionList()
  },
  methods: {
    // 获取负责人列表
    async loadgetDialogOptionList() {
      const res = await getDialogOptionList()
      console.log('获取员工简单列表成功', res)
      this.list = res.data.data
      if (this.isEdit) {
        const { data } = await getDepartmentsById(this.id)
        this.form = data.data
      }
    },
    // 添加
    async doAdd() {
      try {
        console.log(this.id)
        const res = await addDepartments({ ...this.form, pid: this.id })
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    },
    // 修改
    async doEdit(isId) {
      try {
        const res = await editDepartmentsById({ ...this.form, id: isId })
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    },
    // 提交
    hSubmit() {
      this.isEdit ? this.doEdit() : this.doAdd()
      this.$emit('success')
    },
    // 取消
    hCancel() {
      this.$refs.formRef.resetFields()
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$emit('close')
    }
  }
}
</script>

<style>
</style>
